<?php

namespace app\admin\controller;

use app\admin\model\Upload as UploadModel;
use app\admin\model\UserType;
use app\admin\service\Token;
use think\Controller;
use think\Request;
use think\Session;

class Admin extends Controller
{
    protected $beforeActionList = [
        "getPublic" => ["only"=>"warehouse,index,add,edit,delete,database,select,user,sort,status"]
    ];

    private $token = null;
    public function setToken($token){
        $this -> token = $token;
    }

    public function getToken(){
        return $this -> token;
    }

    protected $url = null;

    public function setUrl($url){
        $this -> url = $url;
    }

    public function getUrl(){
        return $this -> url;
    }


    public function getPublic(){
        $request = Request::instance();
        $url = $request->module()."/". $request->controller()."/". $request->action();
        $this -> setUrl($url);
        $token = Token::getToken();
        $this -> setToken($token);
        if($request -> isGet()) {
            if(!$token){
                $this -> error("请重新登录","login/login/login");
            }
            if($url != "admin/Upload/index") {
                if (!$token) {
                    $this->redirect("login/login/login");
                }
                if (Token::getUid() == 0) {
                    $this->error("请先登录","Login/Login/Login");
                }
                if (!UserType::Jurisdiction($url)) {
                    $this->error("没有操作权限");
                }
            }
        }
        $data = $request -> param();
        if(array_key_exists("user_activity",$data)){
            Session::set("user_activity",$data["user_activity"]);
        }
        $user_activity =  Session::get("user_activity");
        $this->assign("user_activity",$user_activity);
        $this -> assign("token",$this -> token);
    }

    //图片
    public static function get_cover($id){
        $upload = new UploadModel();
        $upload = $upload -> find($id)["path"];
        return $upload;
    }

    //验证操作权限
    public function verify_authority(){
        $token = $this -> getToken();
        $url = $this -> getUrl();
        if(!$token){
            return "请重新登录";
        }
        if (Token::getUid() == 0){
            return "请重新登录";
        }
        if($url == ""){
            return "没有操作权限";
        }
        if(!UserType::Jurisdiction($url)){
            return "没有操作权限";
        }
        return "";
    }

    /**
     * 生产订单编号
     */
    public  function makeOrderNo()
    {
        $yCode = range("A","Z");
        $orderSn =
            $yCode[intval(date('Y')) - 2018] . strtoupper(dechex(date('m'))) . date(
                'd') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(
                '%02d', rand(100, 999));
        return $orderSn;
    }

}